package
{
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.utils.getDefinitionByName;
	
	public class PreloaderFactory extends MovieClip
	{
		public function PreloaderFactory()
		{
			stop();
			stage.scaleMode = StageScaleMode.NO_SCALE;
			stage.align = StageAlign.TOP_LEFT;
			addEventListener(Event.ENTER_FRAME, onEnterFrame);
		}
		
		public function onEnterFrame(event:Event):void
		{
			graphics.clear();
			if(framesLoaded == totalFrames)
			{
			removeEventListener(Event.ENTER_FRAME, onEnterFrame);
			nextFrame();
			init();
			}
			else
			{
			var percent:Number = root.loaderInfo.bytesLoaded / root.loaderInfo.bytesTotal;
			graphics.beginFill(0xaaaaaa);
			graphics.drawRect(0, stage.stageHeight/2, stage.stageWidth * percent, 1);
			graphics.endFill();
			}
		}
		
		private function init():void
		{
			var mainClass:Class = Class(getDefinitionByName("Main"));
			if(mainClass)
			{
				var app:Object = new mainClass();
				addChild(app as DisplayObject);
			}
			stage.align = 'top';
			stage.scaleMode = 'noScale';
		}
	}
}